package Operation;

import Book.BookList;

import Book.Book;
import SQLConnection.Conn;

import java.math.BigDecimal;
import java.util.Scanner;

public class AddOperation implements IOOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书!");

        Scanner scan = new Scanner(System.in);
        System.out.print("请输入书名:");
        String name = scan.nextLine();

        System.out.print("请输入作者:");
        String author = scan.nextLine();

        System.out.print("请输入书籍价格:");
        BigDecimal price = scan.nextBigDecimal();

        System.out.print("请输入书籍类别id:");
        int category_id = scan.nextInt();

        Book book = new Book(name, author, price, category_id);

        //检测书架是否有此书
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if (book1.getName().equals(name)) {
                System.out.println("有这本书了！");
                return;
            }
        }
        if (currentSize == bookList.getBooks().length) {
            System.out.println("书架满了，无法再放入!");
        } else {
            bookList.setBook(currentSize, book);
            bookList.setUsedSize(currentSize + 1);
            Conn conn = new Conn();
            conn.addBook(name,author,price,category_id);
        }
    }
}
